package com.m520it.jdmall03.controller;

import java.util.HashMap;

import com.m520it.jdmall03.bean.RResult;
import com.m520it.jdmall03.cons.IdiyMessage;
import com.m520it.jdmall03.cons.NetworkConst;
import com.m520it.jdmall03.util.NetworkUtil;

public class UserController extends BaseController {

	@Override
	protected void handleMessage(int action, Object... values) {
		switch (action) {
		case IdiyMessage.LOGIN_ACTION:
			RResult rResult = loginOrRegist(NetworkConst.LOGIN_URL,
					(String) values[0], (String) values[1]);
			mListener.onModeChanged(IdiyMessage.LOGIN_ACTION_RESULT, rResult);
			break;
		case IdiyMessage.REGIST_ACTION:
			RResult loginOrRegist = loginOrRegist(NetworkConst.REGIST_URL, (String) values[0],
					(String) values[1]);
			mListener.onModeChanged(IdiyMessage.REGIST_ACTION_RESULT, loginOrRegist);
			break;
		}
	}

	private RResult loginOrRegist(String url, String name, String pwd) {
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("username", name);
		params.put("pwd", pwd);
		String jsonStr = NetworkUtil.doPost(url, params);
		RResult rResult = new RResult();
		rResult.setResult(jsonStr);
		return rResult;
	}

}
